package com.hc.util;

import java.io.BufferedInputStream;  
import java.io.BufferedReader;  
import java.io.FileInputStream;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;  
import java.util.zip.ZipFile;  
import java.util.zip.ZipInputStream;  

import com.hc.testLog.TestLogFileBean;

import android.R.integer;


public class ZipFileUtil {

   
    
    public static TestLogFileBean readZipFile(String fileName,String filePath) throws Exception {  
    	
    	TestLogFileBean bean = null;
    	
    	StringBuffer sb = new StringBuffer();
    	
           ZipFile zf = new ZipFile(filePath);  
           InputStream in = new BufferedInputStream(new FileInputStream(filePath));  
           ZipInputStream zin = new ZipInputStream(in);  
           ZipEntry ze;  
           while ((ze = zin.getNextEntry()) != null) {  
               if (ze.isDirectory()) {
               } else {  
//                   System.err.println("file - " + ze.getName() + " : "  
//                           + ze.getSize() + " bytes");  
//                   long size = ze.getSize();  
            	   
            	   
                   if (fileName.indexOf(ze.getName())!=-1) {  
                       BufferedReader br = new BufferedReader(  
                               new InputStreamReader(zf.getInputStream(ze)));  
                       String line;  
                       while ((line = br.readLine()) != null) {  
//                           System.out.println(line);  
                           sb.append(line+"\n");
                       }  
                       br.close();
                       
                       
                       bean = new TestLogFileBean();
                       
                       bean.fileName =  ze.getName();
                       bean.content = sb.toString();
                   }  
//                   System.out.println();  
               }  
           }  
           zin.closeEntry();
		return bean;  
       }  
    
    public static Map<String ,Long> readZipFileName(String file) throws Exception {  
    	
    	Map<String ,Long> zipFileMap = new HashMap<String, Long>();
    	
//        ZipFile zf = new ZipFile(file);  
        InputStream in = new BufferedInputStream(new FileInputStream(file));  
        ZipInputStream zin = new ZipInputStream(in);  
        ZipEntry ze;  
        while ((ze = zin.getNextEntry()) != null) {  
            if (ze.isDirectory()) {
            } else {  
//                System.err.println("file - " + ze.getName() + " : "  
//                        + ze.getSize() + " bytes");  
            
                zipFileMap.put(ze.getName(), ze.getSize());
            }  
        }  
        zin.closeEntry();
		return zipFileMap;  
    }  
}